<?php
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 在线店
 */
class StoreModule extends BasicModule {

	/** @var 模块id */
	private $_moduleid;

	/** @var 用户ID */
	private $_userid;

	/** @var string 表格 */
	private $_table;

	/** @var string 表格数据 */
	private $_tableData;

	/** @var 是否内容分表 */
	private $_split;

	/** @var array 可用字段 */
	private $_fields;


	/**
	 * constructor
	 * @param $moduleid
	 */
	function __construct($moduleid) {
		parent::__construct();
		global $db, $MOD;
		$this->_moduleid  = $moduleid;
		$this->_table     = $db->getPre().'store';
		$this->_tableData = $db->getPre().'store_data';
		$this->_member    = $db->getPre().'member';
		$this->_split     = $MOD['split'];
		$this->_fields = array('userid','company','thumb','catid','business','arbid','areaid','address','telephone','langitude','latitude','catid','status','addtime','edittime');
    }

	public function setUserid($userid) {
		$this->_userid = $userid;
	}

	public function getUserid() {
		return $this->_userid;
	}

	public function getFields() {
		return $this->_fields;
	}
    public function getTable() {
        return $this->_table;
    }

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		if(!is_array($post)) return false;
		if (!$post['thumb']) {
			return $this->err('请上传形象图片');
		}
		/*if (!$post['catid']) {
			return $this->err('请选择在线店类别');
		}*/
		if (!$post['business']) {
			return $this->err('请填写主营范围');
		}
		if (!$post['content']) {
			return $this->err('请填写在线店介绍');
		}
		if (!$post['areaid']) {
			return $this->err('请选择所在地区');
		}
		if (!$post['address']) {
			return $this->err('请填写企业地址');
		}
		if (!$post['address']) {
			return $this->err('请填写联系电话');
		}
		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global $DT_TIME, $_userid;
		$post['addtime'] = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['userid']  = $_userid;
		$post['edittime'] = $DT_TIME;
		$post['content'] = stripslashes($post['content']);
		// catids
		$catids         = implode(',', array_filter($post['catid']));
		if ($catids) {
			$post['catid'] = ',' . $catids . ',';
		}
		// brandids
		if ($post['brandId']) {
			$bids           = implode(',', $post['brandId']);
			$post['arbid'] = ',' . $bids . ',';
		}
		// postion
		$position           = explode(',', $post['position']);
		$post['langitude'] = $position[0];
		$post['latitude']  = $position[1];

		if(!defined('DT_ADMIN')) {
			$content = $post['content'];
			unset($post['content']);
			$post = Str::htmlSpecialChars($post);
			$post['content'] = Str::safe($content);
		}
		$post['content'] = addslashes($post['content']);
		return $post;
	}

	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne() {
		$content_table = content_table($this->_moduleid, $this->_userid, $this->_split, $this->_tableData);
        return $this->_db->getOne("SELECT * FROM {$this->_table} a,{$content_table} c WHERE a.userid=c.userid and a.userid=$this->_userid");
	}


	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = 'status=3', $order = 'addtime DESC', $cache = '') {
		global $MOD,$items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$userinfo  = userinfo($r['userid'],'','*','userid');
			$r['username'] = $userinfo['username'];
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * 根据用户最新登录时间获取列表
	 * @param string $condition
	 * @param string $count_condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getListByLogintime($condition = 'a.userid = m.userid and a.status=3',$count_condition= ' status=3 ', $order = 'm.logintime DESC', $cache = '') {
		global $MOD,$items;
		$num = $this->_db->count($this->_table, $count_condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT a.* FROM {$this->_table} a,{$this->_member} m";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$userinfo  = userinfo($r['userid'],'','*','userid');
			$r['username'] = $userinfo['username'];
			$lists[] = $r;
		}
		return $lists;
	}
	/**
	 * 添加数据
	 * @param $post
	 * @return string
	 */
	function add($post) {
		global $_userid;
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$content_table = content_table($this->_moduleid, $this->_userid, $this->_split, $this->_tableData);
		$this->_db->query("INSERT INTO {$content_table} (userid,content) VALUES ('$this->_userid', '$post[content]')");
		ThumbUpload::clearPicture($this->_moduleid.'_thumb', $post['thumb'], '', $_userid);
		EditorUpload::clearDiff($post['content'], '', $_userid);
		return $this->_userid;
	}

	/**
	 * 编辑
	 * @param $post
	 * @return bool
	 */
	function edit($post) {
		global $_userid,$r;
		$this->delete($this->_userid, false);
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table, $post, "userid={$this->_userid}", '', $this->_fields));
		$content_table = content_table($this->_moduleid, $this->_userid, $this->_split, $this->_tableData);
	    $this->_db->query(Sql::update($content_table,"content='$post[content]'","userid=$this->_userid"));
		ThumbUpload::clearPicture($this->_moduleid.'_thumb', $post['thumb'], $r['thumb'], $_userid);
		EditorUpload::clearDiff($post['content'], $r['content'], $_userid);
		return true;
	}




	/**
	 * 删除条目
	 * @param      $userid
	 * @param bool $all
	 */
	function delete($userid, $all = true) {
		if(is_array($userid)) {
			foreach($userid as $v) { 
				$this->delete($v, $all);
			}
		} else {
			$this->_userid = $userid;
			$r = $this->getOne();
			if($all) {
				if ($r['thumb']) PictureUpload::deletePicture($r['thumb'], $userid);
				if ($r['content']) EditorUpload::clearDiff('', $r['content'], $userid);
				$this->_db->query(Sql::delete($this->_table,"userid=$userid"));
				$content_table = content_table($this->_moduleid, $this->_userid, $this->_split, $this->_tableData);
				$this->_db->query(Sql::delete($content_table,"userid=$userid"));
			}
		}
	}

	/**
	 * 检测
	 * @param $userid
	 * @return bool
	 */
	function check($userid) {
		global $_username, $DT_TIME;
		if(is_array($userid)) {
			foreach($userid as $v) { $this->check($v); }
		} else {
			$this->_userid = $userid;
			$this->_db->query(Sql::update($this->_table,"status=3,edittime=$DT_TIME","userid=$userid"));
			return true;
		}
	}

	/**
	 * 未通过审核
	 * @param $userid
	 * @return bool
	 */
	function reject($userid) {
		global $_username, $DT_TIME;
		if(is_array($userid)) {
			foreach($userid as $v) { $this->reject($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=1","userid=$userid"));
			return true;
		}
	}


}
